﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Collections;

namespace Deloitte.Controls
{
    public partial class BindingListFilterControl<T> : UserControl
    {
        private BindingList<T> _fullList = new BindingList<T>();

        public BindingListFilterControl(BindingList<T> list)
        {
            InitializeComponent();            
            this.List = list;
            this.filterToolStripButton.Click += new EventHandler(filterToolStripButton_Click);
        }       

        void filterToolStripButton_Click(object sender, EventArgs e)
        {
            FilterList(true);
        }

        private void FilterList(bool usingFilterText)
        {
            if (usingFilterText)
            {
                string propName = this.propertyStripComboBox1.Text;
                string val = this.filterToolStripTextBox.Text;
                if (!string.IsNullOrEmpty(propName) && !string.IsNullOrEmpty(val))
                {
                    //get the type of the property
                    PropertyInfo prop = _list[0].GetType().GetProperty(propName);
                    if (prop.PropertyType.Name.ToUpper().Equals("STRING"))
                    {                        
                        this._filteredList = Deloitte.Generics<T>.ToBindingList((from item in this._list where ((string)item.GetType().GetProperty(propName).GetValue(item, null)).ToUpper().IndexOf(val.ToUpper()) > -1 select item).ToList());
                    }
                    else if (prop.PropertyType.Name.ToUpper().Equals("BOOLEAN"))
                    {
                        if (val.ToUpper().Equals("TRUE"))
                        {
                            this._filteredList = Deloitte.Generics<T>.ToBindingList((from item in this._list where ((bool)item.GetType().GetProperty(propName).GetValue(item, null)).Equals(true) select item).ToList());
                        }
                        else if (val.ToUpper().Equals("FALSE"))
                        {
                            this._filteredList = Deloitte.Generics<T>.ToBindingList((from item in this._list where ((bool)item.GetType().GetProperty(propName).GetValue(item, null)).Equals(false) select item).ToList());
                        }
                    }        
                }
            }
            else
            {
                this._filteredList = this._fullList;
            }
            if (ListFilteredEvent != null) this.ListFilteredEvent(this, new ListFilteredEventArgs(this._filteredList));
        }

        BindingList<T> _list;

        public BindingList<T> List
        {
            get { return _list; }
            set { 
                _list = value;
                if (_list != null)
                {
                    _fullList = _list;
                    SetPropertyDropDown(_list);
                }
            }
        }

        private void SetPropertyDropDown(BindingList<T> list)
        {
            propertyStripComboBox1.Items.Clear();
            if (list != null)
            {                
                
                if (list.Count > 0)
                {                   
                    Deloitte.Reflection.AddPropertiesToComboBox(list[0].GetType(), propertyStripComboBox1, AutoCompleteMode.SuggestAppend);
                }              
            }
        }

        BindingList<T> _filteredList = null;
        public event ListFilteredEventHandler ListFilteredEvent;

        protected virtual void OnListFiltered(ListFilteredEventArgs e)
        {
            if (ListFilteredEvent != null)
            {
                ListFilteredEvent(this, e);
            }
        }           

        public delegate void ListFilteredEventHandler(object sender, ListFilteredEventArgs e);
        public class ListFilteredEventArgs: EventArgs
        {
            public ListFilteredEventArgs(BindingList<T> list)
            {
                this._filteredList = list;
            }
            private BindingList<T> _filteredList;

            public BindingList<T> FilteredList
            {
                get { return _filteredList; }                
            }
        }

        private void clearToolStripButton_Click(object sender, EventArgs e)
        {
            this.FilterList(false);            
        }

        private void filterToolStripButton_Click_1(object sender, EventArgs e)
        {

        }

      
    }
}
